'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE } = app.Sequelize;

  const JobApplication = app.model.define('job_application', {
    applicationId: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      field: 'application_id',
      comment: '投递编号'
    },
    jobId: {
      type: INTEGER,
      allowNull: false,
      field: 'job_id',
      comment: '岗位编号'
    },
    enterpriseId: {
      type: INTEGER,
      allowNull: false,
      field: 'enterprise_id',
      comment: '企业编号'
    },
    userId: {
      type: INTEGER,
      allowNull: false,
      field: 'user_id',
      comment: '用户编号'
    },
    userName: {
      type: STRING(50),
      allowNull: false,
      field: 'user_name',
      comment: '用户姓名'
    },
    resumeId: {
      type: INTEGER,
      allowNull: true,
      field: 'resume_id',
      comment: '简历编号'
    },
    phone: {
      type: STRING(20),
      allowNull: false,
      comment: '手机号'
    },
    email: {
      type: STRING(100),
      allowNull: true,
      comment: '邮箱'
    },
    resumeUrl: {
      type: STRING(500),
      allowNull: true,
      field: 'resume_url',
      comment: '简历文件URL'
    },
    coverLetter: {
      type: TEXT,
      allowNull: true,
      field: 'cover_letter',
      comment: '求职信'
    },
    status: {
      type: STRING(20),
      allowNull: false,
      defaultValue: 'pending',
      comment: '投递状态：pending-待处理，interviewing-面试中，hired-已录用，rejected-已拒绝'
    },
    progress: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '进度百分比'
    },
    interviewTime: {
      type: DATE,
      allowNull: true,
      field: 'interview_time',
      comment: '面试时间'
    },
    hireTime: {
      type: DATE,
      allowNull: true,
      field: 'hire_time',
      comment: '录用时间'
    },
    applyTime: {
      type: DATE,
      allowNull: false,
      field: 'apply_time',
      defaultValue: app.Sequelize.NOW,
      comment: '投递时间'
    }
  }, {
    tableName: 'job_application',
    timestamps: false,
    comment: '岗位投递记录表'
  });

  // 定义关联关系
  JobApplication.associate = function() {
    // 投递记录属于岗位
    app.model.JobApplication.belongsTo(app.model.Job, {
      foreignKey: 'jobId',
      targetKey: 'jobId',
      as: 'job'
    });
    
    // 投递记录属于简历
    app.model.JobApplication.belongsTo(app.model.Resume, {
      foreignKey: 'resumeId',
      targetKey: 'id',
      as: 'resume'
    });
    
    // 投递记录属于用户
    app.model.JobApplication.belongsTo(app.model.User, {
      foreignKey: 'userId',
      targetKey: 'id',
      as: 'user'
    });
  };

  return JobApplication;
};











































